﻿using CCWin;
using System;
using System.Data;
using System.Windows.Forms;
namespace DC.Views
{
    public partial class Form_ProjectManagement : Skin_DevExpress
    {
        public Form_ProjectManagement()
        {
            InitializeComponent();
        }
        Utils.Database DB = Utils.Global.GlobalDB;

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            searchProject();
        }
        private void searchProject()
        {
            string sql = "select * from Project where 1=1 ";
            if (textBox1.Text != string.Empty)
            {
                sql += "and (Project_Abbreviation like'%" + textBox1.Text + "%' or Project_Name like'%" + textBox1.Text + "%' )";
            }
            
            //MessageBox.Show(sql);
            DataSet projectSet = DB.GetDs(sql);
            this.ProjectList.BeginUpdate();
            ProjectList.Items.Clear();

            for (int i = 0; i < projectSet.Tables[0].DefaultView.Count; i++)
            {
                DataRow row = projectSet.Tables[0].DefaultView[i].Row;
                ListViewItem item = new ListViewItem(row["Project_Name"].ToString());
                item.Tag = row["Project_ID"].ToString();
                item.SubItems.Add(row["Project_Price"].ToString());
                item.SubItems.Add(row["Project_Suffix"].ToString());
                this.ProjectList.Items.Add(item);
            }
            this.ProjectList.EndUpdate();
        }

        private void Form_ProjectManagement_Load(object sender, EventArgs e)
        {
            loadProjectList();

        }

        private void loadProjectList()
        {
            DataSet projectSet = DB.GetDs("select * from Project");
            this.ProjectList.BeginUpdate();
            ProjectList.Items.Clear();
            for (int i = 0; i < projectSet.Tables[0].DefaultView.Count; i++)
            {
                DataRow row = projectSet.Tables[0].DefaultView[i].Row;
                ListViewItem item = new ListViewItem(row["Project_Name"].ToString());
                item.Tag = row["Project_id"].ToString();
                item.SubItems.Add(row["Project_Price"].ToString());
                item.SubItems.Add(row["Project_Suffix"].ToString());
                item.SubItems.Add(row["Project_Father"].ToString());
                this.ProjectList.Items.Add(item);
            }
            ProjectList.Columns[0].Width = 225;
            ProjectList.Columns[1].Width = 225;
            ProjectList.Columns[2].Width = 225;
            ProjectList.Columns[3].Width = 225;
            this.ProjectList.EndUpdate();
        }
        

        private void skinButton1_Click(object sender, EventArgs e)
        {
            Views.Form_ProjectInfo ProjectInfo = new Views.Form_ProjectInfo();
            ProjectInfo.Lltrans = 0;
            ProjectInfo.ShowDialog();
            loadProjectList();

        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            //删除项目
            if (ProjectList.CheckedItems.Count==0)
            {
                MessageBox.Show("未选中任何项目", "报错", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            DialogResult R = MessageBox.Show("确认删除选中的项目吗?", "删除项目", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (R == DialogResult.Yes)
            {
                
                for (int i = 0; i < ProjectList.CheckedItems.Count; i++)
                {
                    string sql = @"delete from Project where Project_ID=" + ProjectList.CheckedItems[i].Tag.ToString();
                    DB.Execute(sql);
                }
                loadProjectList();
            }

        }

        private void ProjectList_DoubleClick(object sender, EventArgs e)
        {
            if (ProjectList.SelectedItems.Count == 0)
            {
                return;
            }
            int Project_ID = int.Parse(ProjectList.SelectedItems[0].Tag.ToString());
            Views.Form_ProjectInfo ProjectInfo = new Views.Form_ProjectInfo();
            ProjectInfo.Lltrans = Project_ID;
            ProjectInfo.ShowDialog();
            loadProjectList();

        }

        
        private void skinButton3_Click(object sender, EventArgs e)
        {
            if (ProjectList.CheckedItems.Count == 0)
            {
                MessageBox.Show("没有选择项目，请选中多个项目！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Views.Form_ProjectFatherEdit projectFatherEdit = new Form_ProjectFatherEdit();
            for (int i = 0; i < ProjectList.CheckedItems.Count; i++)
            {
                projectFatherEdit.range.Add(Convert.ToInt32(ProjectList.CheckedItems[i].Tag));
            }
            projectFatherEdit.ShowDialog();
            loadProjectList();

        }

        private void skinButton4_Click(object sender, EventArgs e)
        {
            //全选
            for (int i = 0; i < ProjectList.Items.Count; i++)
            {   
                ProjectList.Items[i].Checked = true;
            }
        }

        private void skinButton5_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < ProjectList.Items.Count; i++)
            {
                ProjectList.Items[i].Checked = !ProjectList.Items[i].Checked;
            }
        }
    }
}
